﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Utility2DLibrary
{
    public class Point
    {
        protected float x;
        [ContentSerializerIgnore]
        public float X
        {
            get { return x; }
            set { x = value; }
        }
        protected float y;
        [ContentSerializerIgnore]
        public float Y
        {
            get { return y; }
            set { y = value; }
        }
        public Point()
        {
            x = y = -1;
        }
        public Point(float xPos, float yPos)
        {
            x = xPos;
            y = yPos;
        }
        public Point(Point point)
        {
            this.x = point.X;
            this.y = point.Y;
        }
        public Point(Vector2 vector)
        {
            this.x = vector.X;
            this.y = vector.Y;
        }
        public void Translate(Vector2 displacement)
        {
            this.x += displacement.X;
            this.y += displacement.Y;
        }
        public void Scale(Vector2 scaleFactors)
        {
            this.x *= scaleFactors.X;
            this.y *= scaleFactors.Y;
        }
        public Point TranslateAndTransform(Vector2 scale,Vector2 displacement, float theta, Point origin)
        {
            Point newPoint = new Point(this);
            newPoint.Scale(scale);
            newPoint.Translate(displacement);
            newPoint.Transform(origin, theta);
            return newPoint;
        }
        public void Transform(Point origin, double theta)
        {
            theta = -theta; // why is this equation rotating things backwards??? reverse the sign here to correct
            Point normalPoint = Point.Normalize(this, origin);
            x = (float)(Math.Cos(theta)) * normalPoint.X - (float)(Math.Sin(theta)) * normalPoint.Y;
            y = (float)(Math.Sin(theta)) * normalPoint.X + (float)(Math.Cos(theta)) * normalPoint.Y;
            Translate(origin.ToVector2());
            //x = normalPoint.x;
            //y = normalPoint.y;
            //this.X = normalPoint.X + origin.X;
            //this.Y = normalPoint.Y + origin.Y;
        }
        public static Point Normalize(Point originalPoint, Point originPoint)
        {
            return new Point(originalPoint.X - originPoint.X, originalPoint.Y - originPoint.Y);
        }
        public Vector2 ToVector2()
        {
            return new Vector2(x, y);
        }
        public Point FlipY()
        {
            Point newPoint = new Point(this);
            newPoint.y *= -1;
            return newPoint;
        }
        public VertexPositionColor ToVertexColorObject(Color vertexColor)
        {
            return new VertexPositionColor(new Vector3(x,y,0),vertexColor);
        }
    }
}
